package base;

import com.example.costs.AddTransActivity;
import com.example.costs.MainActivity;

/**
 * Created by Developer on 22.06.13.
 */
public class Category {
//    id пользователя
    private Integer uId;
//    id категории
    private Integer ctgId;
//    наименование
    private String name;
//    тип e-расходы; i-доходы
    private String type;

    public Category(Integer uId, Integer ctgId, String name, String type) {
        if (uId == null){
            new NullPointerException("uId in Category is null");
        }
        this.uId = uId;
        this.type =  type == null ? "e" : type;
        if (ctgId == null){
            if (name != null){
                ctgId = getCtgIdByName(this.uId,name);
                if(ctgId == null){
                    ctgId = MainActivity.db.addCtg(uId,name,type);
                }
            }else{
                new Exception("ctgId and name is null, дыбил");
            }
        }
        this.ctgId = ctgId;
        this.name = name;
    }

    public Integer getuId() {
        return uId;
    }

    public void setuId(Integer uId) {
        this.uId = uId;
    }

    public Integer getCtgId() {
        return ctgId;
    }

    public void setCtgId(Integer ctgId) {
        this.ctgId = ctgId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private Integer getCtgIdByName(Integer uid, String name){
        return MainActivity.db.getCtgIdByName(uid,name);
    }

    private void getCtgById(Integer uid, Integer ctgId){
        Category ctg = MainActivity.db.getCtgById(uid,ctgId);
        if(ctg == null){
            new Exception("User "+uid+" do not have categoty "+ctgId);
        } else{
            this.ctgId = ctg.getCtgId();
            this.uId = ctg.getuId();
            this.name = ctg.getName();
            this.type = ctg.getType();
        }

    }

    @Override
    public String toString(){
        return "uId - "+uId+"; ctgId - "+ctgId+"; name - "+name+"; type - "+type+";";
    }
}
